package grex.Nodes.Depricated;

import grex.Nodes.Depricated.ConVar;
import grex.Options;
import grex.Data.Prediction;
import grex.Data.PredictionContainer;
import grex.Nodes.GeneException;
import grex.Nodes.Node;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Hashtable;

public class ConVal extends Node {

    private ArrayList<double[]> values;
    Hashtable<Double, Integer> rightClassCount = new Hashtable<Double, Integer>();
    Hashtable<Double, Integer> leftClassCount = new Hashtable<Double, Integer>();
    boolean optimized = false;
    private Node neighbour;
    private double value = 0;
    private double internalValue = -1;
    private int col = 0;

    public ConVal() {
        super(0);
        internalValue = Options.rnd.nextDouble();
    }

    public ConVal(double value, double internalValue) {
        super(0);
        this.value = value;
        this.internalValue = internalValue;
    }

    public Node getRoot() {
        if (parent == null) {
            return this;
        }
        Node p = parent;
        while (p.getParent() != null && p != p.getParent()) {
            p = p.getParent();
        }
        return p;
    }

    public synchronized void execute(Node parent, PredictionContainer data, int mode) throws GeneException {
        this.parent = parent;

        if (mode == Node.TRAIN_MODE) {

            Node[] childs = parent.getChildren();
            col = ((ConVar) (childs[0] == this ? childs[1] : childs[0])).getVarNr();
            value = environment.getInternalNumericalValue(col, internalValue,getFold());
        }
        for (Prediction p : data.values()) {
            p.setPrediction(value);
        }

    }

    private void setNeighbour(Node neighbour) {
        this.neighbour = neighbour;
    }

    private void setCol(int col) {
        this.col = col;
    }

    public String toString() {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(3);
        return "" + nf.format(value);
    }

    public Object clone() {
        ConVal newConVal = new ConVal(value, internalValue);
        newConVal.setFold(getFold());
        newConVal.setOptions(ops);
        newConVal.setEnvironment(environment);
        newConVal.setNeighbour(neighbour);
        newConVal.setCol(col);
        newConVal.setParent(parent);
        return newConVal;
    }
}
